import random
import time

import win32gui
import win32api
import win32con
from pykeyboard import PyKeyboard
# 多线程
from threading import Thread

# from pymouse import PyMouse
hwnd_id = []


def get_all_hwnd(hwnd, mouse):
    if (win32gui.IsWindow(hwnd) and
        win32gui.IsWindowEnabled(hwnd) and
        win32gui.IsWindowVisible(hwnd)) and win32gui.GetWindowText(hwnd).__contains__('Hollow'):
        hwnd_id.append(hwnd)


win32gui.EnumWindows(get_all_hwnd, 0)

handle = hwnd_id[0]

# 激活窗口
# win32gui.SetForegroundWindow(handle)

k = PyKeyboard()
moves = [k.up_key, k.down_key, k.left_key, k.right_key]
jumps = [0, 1]  # 0: jump, 1: not jump
skills = [0, 1, 2, 3, 4]  # 0: 等待 1: 技能F 2: 冲刺C 3: 攻击X 4: 剑技
jj_flag = [False]


def jj(flag):
    flag[0] = True
    k.press_key('x')
    time.sleep(1.8)
    k.release_key('x')
    flag[0] = False


for i in range(100000):
    # 如果当前激活的窗口不是游戏窗口，就continue
    time.sleep(0.2)
    if win32gui.GetForegroundWindow() != handle:
        continue
    print(jj_flag)
    # 随机动
    move_act = random.randint(0, 3)
    move = moves[move_act]
    k.tap_key(move)
    # 随机跳跃
    jump_act = random.randint(0, 1)
    if jump_act == 0:
        k.press_key('z')
    elif jump_act == 1:
        k.release_key('z')
    # 随机技能
    skill_act = random.randint(0, 4)
    if skill_act == 0:
        pass
    elif skill_act == 1 and not jj_flag[0]:
        k.tap_key('f')
    elif skill_act == 2:
        k.tap_key('c')
    elif skill_act == 3 and not jj_flag[0]:
        k.tap_key('x')
    elif skill_act == 4 and not jj_flag[0]:
        # 多线程执行 jj()
        t = Thread(target=jj, args=(jj_flag,))
        t.start()

# m = PyMouse()

# 激活窗口 桌面 001.txt - 记事本
# 如果已经打开了，就激活，如果没有打开，就打开
# if '001.txt - 记事本' in hwnd_title.keys():
#     handle = hwnd_title['001.txt - 记事本']
#     win32gui.ShowWindow(handle, win32con.SW_SHOWNORMAL)
#     win32gui.SetForegroundWindow(handle)
#     # 光标移动到最后
#     # 输入10个w
#     k = PyKeyboard()
#     k.type_string('wwwwwwwwww')
# else:
#     # 打开桌面 001.txt,  C:\Users\Administrator\Desktop\001.txt
#     win32api.ShellExecute(0, 'open', 'C:\\Users\\Administrator\\Desktop\\001.txt', '', '', 1)
#     # 激活
#     while not '001.txt - 记事本' in hwnd_title.keys():
#         time.sleep(0.1)
#         win32gui.EnumWindows(get_all_hwnd, 0)
#     # 找到title为 001.txt - 记事本 的窗口
#     handle = hwnd_title['001.txt - 记事本']
#     win32gui.ShowWindow(handle, win32con.SW_SHOWNORMAL)
#     # 输入10个Y
#     k = PyKeyboard()
#     k.type_string('YYYYYYYYYY')
